package com.uu.it.tracker.sensordata;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.uu.it.tracker.computation.TrackerData;
import com.uu.it.tracker.computation.Calculator.StatusBand;

import android.util.Log;

public class SensorReader {
	
	/**
	 * file input stream
	 * */
	private BufferedReader input;
	
/*	*//**
	 * MASK to convert byte to integer
	 * *//*
	private static final int MASK = 0xFF;*/
	
	/**
	 * Sensor accessing interface
	 * */
	private static final String SENSORFILENAME = "/dev/ttyUSB0";
	
	private static final int LEGDATA = 1;
	
	private static final int ARMDATA = 0; 
	
	private SensorDataStack legData = new SensorDataStack();
	
	private SensorDataStack armData = new SensorDataStack();
	
	class SensorDataStack {
		/**
		 * the first element is the previous value
		 * */
		private SensorData[] dataList = new SensorData[2];
		private int length = 0;
		
		public void insert(SensorData data){
			dataList[0] = dataList[1];
			dataList[1] = data;
			if(length < 2) length++;
		}
		public int length(){
			return length;
		}
		public SensorData[] getStack(){
			return dataList;
		}
	}
	
	/**
	 * Open file and generate the input stream
	 * */
	public void start() {
	     File sensorFile = new File(SENSORFILENAME);
	     try {
			InputStream is = new FileInputStream(sensorFile);
			input = new BufferedReader(new InputStreamReader(is));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Close the file
	 * */
	public void close() {
		try {
			input.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Read a piece of data from the sensor
	 * */
	public TrackerData read() throws IOException {
		//is.read(dataStream);
		//Log.i("Msg", "try to read data from sensor");
        String line;
        line = input.readLine();
        Log.i("msg", line);
       
        return parseIncomingLine(line);
	}
	
/*	*//**
	 * Convert bytes into an integer, big endian
	 * *//*
	private int bytesToInt(byte[] bytes, int offset, int length){
		int fromByte = 0;
		for(int i = 0; i < length; i++) {
			int n = (int)(bytes[offset + i] & MASK) << (8 * (length - 1 - i));
			fromByte += n;
		}
		return fromByte;
	}*/
	
	private TrackerData parseIncomingLine(String line) {
			TrackerData data = null;
			
		    if (line == null) {
		      Log.i("No data", "No data");
		      return null;
		    }
		    /* Split line into components */
		    String[] components = line.split(" ");
		    
		    if(components.length < 4) return null;
		    
		    int id = Integer.parseInt(components[0]);
		    int x = Integer.parseInt(components[1]);
		    int y = Integer.parseInt(components[2]);
		   
		    switch(id){
		    case ARMDATA:	
		    	armData.insert(new ArmSensorData(x, y));
		    	break;
		    case LEGDATA:
		    	legData.insert(new LegSensorData(x, y));
		    	break;
		    default:
		    	return data =  null;
		    }
		    
		    if((armData.length() == 2) && (legData.length() == 2)){
		    	//generate trackerData
		    	SensorData[] armDataList = armData.getStack();
		    	SensorData[] legDataList = legData.getStack();
		    	data = new TrackerData(armDataList[0], armDataList[1],
		    			legDataList[0], legDataList[1]);
		    }
		    else data = null;
		    return data;
	}

}
